package com.lagou.task19;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLtest {
    public static void main(String[] args) {
        try {
//        1使用参数指定的字符串来构造对象
            URL url1 = new URL("https://kaiwu.lagou.com/");
//        2获取相关信息并打印
            String s1 = url1.getProtocol();
            String s2 = url1.getHost();
            String s3 = url1.getPath();
            int s4 = url1.getPort();
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);

//            3建立连接读取相关信息
            HttpURLConnection hurl = (HttpURLConnection) url1.openConnection();
            InputStream is1 = hurl.getInputStream();
            BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
            String str = null;
            while ((str = br1.readLine()) != null){
                System.out.println(str);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
